package com.dadi.broadcastdemo1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

/**
 * author: 韦敏敏
 * date: 2019/12/29 0029
 */
public class MainFragment1 extends Fragment {
    TextView tvContent;
    MyReceiver mReceiver = new MyReceiver();

    public MainFragment1() {
    }

    @Override
    public void onResume() {
        super.onResume();
        // 接收系统广播，不能用LocalBroadcastReceiver
        IntentFilter myIF =  new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED");
        myIF.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
        myIF.addAction("android.intent.action.BATTERY_LOW");
        myIF.addAction("android.intent.action.BATTERY_OKAY");
        getActivity().registerReceiver(mReceiver, myIF);
//        LocalBroadcastManager.getInstance(getContext()).registerReceiver(mReceiver, myIF);
        Log.v(TextActivity.TAG, "receive should be register");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.v(TextActivity.TAG, "receiver should be unregister");
        getActivity().unregisterReceiver(mReceiver);
//        LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mReceiver);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.layout_fragment1,container, false);
tvContent = myView.findViewById(R.id.textView1);
        return myView;
    }
    public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v(TextActivity.TAG, "receive an intent");
            String info = "\n something wrong.";
            int mStatus = 0;
            if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)){ // battery is low;
                info = "\n batter low, should shut down things.";
                mStatus = 1;
                Log.v(TextActivity.TAG, "battery low");
            } else if(intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)){ // battery is now ok!
                info = "\n battery Okey. ";
                mStatus = 2;
                Log.v(TextActivity.TAG, "battery okay");
            } else if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){ // battery is charging
                info = "\n Power connected, so phone is charging";
                mStatus = 3;
                Log.v(TextActivity.TAG, "ac on");
            } else if(intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)){ // power has been disconnected
                info = "\n Power disconnected";
                mStatus = 4;
                Log.v(TextActivity.TAG, "ac off");
            }
            tvContent.setText(mStatus + info);
        }
    }
}
